package ${packageCode}.controller

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.google.common.collect.Lists
import ${packageCode}.${moduleMapperCode}.dto.${businessCode}Dto
import ${packageCode}.${moduleMapperCode}.entity.${businessCode}
import ${packageCode}.${moduleMapperCode}.service.${businessCode}Service
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.apache.commons.collections.CollectionUtils
import org.apache.commons.lang3.StringUtils
import org.apache.dubbo.config.annotation.DubboReference
import org.springframework.beans.BeanUtils
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import top.cardone.context.ApplicationContextHolder

import java.util.stream.Collectors

@Api(tags = "v1:${moduleName}-${businessName}")
@RestController
@RequestMapping("/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}")
class V1${businessCode}Controller {
    @DubboReference
    ${businessCode}Service ${(businessCode?uncap_first)!''}Service

    @ApiOperation(value = "查询:分页")
    @PostMapping(["/page/index"])
    org.springframework.data.domain.Page<${businessCode}Dto> page(@RequestBody Map<String, Object> pageParam) {
        ${(businessCode?uncap_first)!''}Service.page(pageParam)
    }

    @ApiOperation(value = "查询:列表:下拉选择")
    @PostMapping(["/list/options"])
    List<Map<String, Object>> list(@RequestBody Map<String, Object> listParam) {
        List<${businessCode}> ${(businessCode?uncap_first)!''}List = ${(businessCode?uncap_first)!''}Service.list(listParam)

        if (CollectionUtils.isEmpty(${(businessCode?uncap_first)!''}List)) {
            return []
        }

        def listReturn = ${(businessCode?uncap_first)!''}List.stream().map({ it ->
            [
                    id   : it.${(businessCode?uncap_first)!''}Id,
                    code : it.${(businessCode?uncap_first)!''}Code,
                    value: StringUtils.defaultString(it.zhCnName, it.enName),
                    name : StringUtils.defaultString(it.zhCnName, it.enName)
            ]
        }).collect(Collectors.toList())

        listReturn
    }

    @ApiOperation(value = "查询:单个:编辑")
    @PostMapping(["/getById/modify"])
    ${businessCode}Dto getByIdForModify(@RequestBody Map<String, Object> getParam) {
        if (StringUtils.isBlank(getParam.${(businessCode?uncap_first)!''}Id)) {
            throw new Exception("${businessName}标识不能为空值")
        }

        ${businessCode} ${(businessCode?uncap_first)!''} = ${(businessCode?uncap_first)!''}Service.getById(getParam.${(businessCode?uncap_first)!''}Id)

        if (!${(businessCode?uncap_first)!''}) {
            throw new Exception("未查询到${businessName}")
        }

        ${businessCode}Dto ${(businessCode?uncap_first)!''}Dto = new ${businessCode}Dto()

        BeanUtils.copyProperties(${(businessCode?uncap_first)!''}, ${(businessCode?uncap_first)!''}Dto)

        ${(businessCode?uncap_first)!''}Dto
    }

    @ApiOperation(value = "查询:单个:查看")
    @PostMapping(["/getById/view"])
    ${businessCode}Dto getByIdForView(@RequestBody Map<String, Object> getParam) {
        if (StringUtils.isBlank(getParam.${(businessCode?uncap_first)!''}Id)) {
            throw new Exception("${businessName}标识不能为空值")
        }

        ${businessCode} ${(businessCode?uncap_first)!''} = ${(businessCode?uncap_first)!''}Service.getById(getParam.${(businessCode?uncap_first)!''}Id)

        if (!${(businessCode?uncap_first)!''}) {
            throw new Exception("未查询到${businessName}")
        }

        ${businessCode}Dto ${(businessCode?uncap_first)!''}Dto = new ${businessCode}Dto()

        BeanUtils.copyProperties(${(businessCode?uncap_first)!''}, ${(businessCode?uncap_first)!''}Dto)

        ${(businessCode?uncap_first)!''}Dto
    }

    @ApiOperation(value = "删除:单个")
    @PostMapping(["/removeById"])
    Map<String, Object> removeById(@RequestBody Map<String, Object> removeParam) {
        if (StringUtils.isNotBlank(removeParam.${(businessCode?uncap_first)!''}Id)) {
            return [
                    delete: ${(businessCode?uncap_first)!''}Service.removeById(removeParam.${(businessCode?uncap_first)!''}Id)
            ]
        }

        if (StringUtils.isNotBlank(removeParam.${(businessCode?uncap_first)!''}Ids)) {
            return [
                    delete: ${(businessCode?uncap_first)!''}Service.removeByIds(Lists.newArrayList(StringUtils.split(removeParam.${(businessCode?uncap_first)!''}Ids, ",")))
            ]
        }

        throw new Exception("未传参${businessName}标识")
    }

    @ApiOperation(value = "删除:多个")
    @PostMapping(["/removeByIds"])
    Map<String, Object> removeByIds(@RequestBody Map<String, Object> removeParam) {
        return this.removeById(removeParam)
    }

    @ApiOperation(value = "保存:单个")
    @PostMapping(["/save"])
    Map<String, Object> save(@RequestBody ${businessCode}Dto saveParam) {
        if (StringUtils.isBlank(saveParam.${(businessCode?uncap_first)!''}Code)) {
            throw new Exception("${businessName}编号不能为空值")
        }

        if (StringUtils.isBlank(saveParam.zhCnName)) {
            throw new Exception("${businessName}中文名称不能为空值")
        }

        ${businessCode} ${(businessCode?uncap_first)!''} = ${(businessCode?uncap_first)!''}Service.getBy${businessCode}Code(saveParam.${(businessCode?uncap_first)!''}Code)

        if (${(businessCode?uncap_first)!''}) {
            if (!StringUtils.equals(saveParam.${(businessCode?uncap_first)!''}Id, ${(businessCode?uncap_first)!''}.${(businessCode?uncap_first)!''}Id)) {
                throw new Exception("${businessName}编号已存在")
            }
        }

        [
                save: ${(businessCode?uncap_first)!''}Service.save(saveParam)
        ]
    }

    @ApiOperation(value = "更新:单个")
    @PostMapping(["/updateById"])
    Map<String, Object> updateById(@RequestBody ${businessCode}Dto updateParam) {
        if (StringUtils.isBlank(updateParam.${(businessCode?uncap_first)!''}Id)) {
            throw new Exception("${businessName}标识不能为空值")
        }

        if (StringUtils.isBlank(updateParam.${(businessCode?uncap_first)!''}Code)) {
            throw new Exception("${businessName}编号不能为空值")
        }

        if (StringUtils.isBlank(updateParam.zhCnName)) {
            throw new Exception("${businessName}中文名称不能为空值")
        }

        ${businessCode} ${(businessCode?uncap_first)!''} = ${(businessCode?uncap_first)!''}Service.getBy${businessCode}Code(updateParam.${(businessCode?uncap_first)!''}Code)

        if (${(businessCode?uncap_first)!''}) {
            if (!StringUtils.equals(updateParam.${(businessCode?uncap_first)!''}Id, ${(businessCode?uncap_first)!''}.${(businessCode?uncap_first)!''}Id)) {
                throw new Exception("${businessName}编号已存在")
            }
        }

        [
                update: ${(businessCode?uncap_first)!''}Service.updateById(updateParam)
        ]
    }
}
